﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.ProcessInterfaces;

namespace XWG.JWBST.RegisterProcess
{
    /// <summary>
    /// 用户注册
    /// </summary>
    public class Process : IProcess
    {
        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="reader">接受的数据</param>
        /// <param name="context">请求信息</param>
        /// <returns>操作结果</returns>
        public string ProcessData(StreamReader reader, HttpContext context)
        {
            //读取提交数据并UrlDecode解码
            string receivemes = HttpUtility.UrlDecode(reader.ReadToEnd());

            //反序列化json对象
            JObject ob = (JObject)JsonConvert.DeserializeObject(receivemes);
            TbBstUser regUser = new TbBstUser();
            TbBstUserImei userImei = new TbBstUserImei();
            regUser.PhoneNo = ob["phone"].ToString().Replace("\"", string.Empty);
            if (regUser.PhoneNo != string.Empty)
            {
                //renum     1  已经注册   0 尚未注册
                int renum = XWG.JWBST.JWBSTBLL.TbBstUserBLL.SelectUser(regUser.PhoneNo);
                if (renum == 0)
                {
                    regUser.UserId = Guid.NewGuid().ToString();
                    regUser.PassWord = ob["password"].ToString().Replace("\"", string.Empty);
                    regUser.IsActive = "0";
                    regUser.RegDate = DateTime.Now;
                    regUser.LastLoginDate = regUser.RegDate;
                    userImei.ImeiPkid = Guid.NewGuid().ToString();
                    userImei.UserId = regUser.UserId;
                    userImei.Imei = ob["imei"].ToString().Replace("\"", string.Empty);
                    userImei.RegDate = regUser.RegDate;
                    try
                    {
                        //通过事物进行数据插入
                        bool IsSucceed = false;
                        List<object> items = new List<object>();
                        List<int> op = new List<int>();
                        items.Add(regUser);
                        op.Add((int)XWG.Framework.AuthSPConst.OP.Create);
                        items.Add(userImei);
                        op.Add((int)XWG.Framework.AuthSPConst.OP.Create);
                        XWG.JWBST.JWBSTBLL.Tran.TransOP(ref IsSucceed, items, op);
                        if (IsSucceed)
                        {
                            return "操作成功！";
                        }
                        return "操作失败!";
                        
                    }
                    catch (Exception ex)
                    {
                        return ex.Message;
                    }
                }
                else
                {
                    return "错误：此用户已注册";
                }
            }
            else
            {
                return "错误：请输入手机号";
            }
        }

        ///// <summary>
        ///// 验证码图片生成
        ///// </summary>
        ///// <param name="Vci"></param>
        ///// <returns></returns>
        //private static string GetVCode()
        //{
        //    VCodeImage Vci = new VCodeImage();
        //    VerificationCode VCode = new VerificationCode();
        //    Bitmap image = VCode.GetVerificationCode();
        //    string code = VCode.CheckCode();
        //    MemoryStream mstream = new MemoryStream();
        //    image.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp);
        //    Vci.ImageData = mstream.ToArray();

        //    string json = JsonConvert.SerializeObject(Vci);
        //    return json;
        //}
        //public class VCodeImage
        //{
        //    public byte[] ImageData { get; set; }
        //}
    }
}
